const { request } = require('./api');

/**
 * 倒数日API接口
 */
const countdownApi = {
  // 基础URL
  baseUrl: '/tea/countdown',

  /**
   * 获取全部倒数日事件
   */
  async getList() {
    try {
      const response = await request(`${this.baseUrl}/list`);
      // 统一处理响应格式
      if (Array.isArray(response)) {
        return { code: 200, data: response };
      } else if (response && response.code) {
        return response;
      } else {
        return { code: 200, data: [] };
      }
    } catch (error) {
      console.error('获取倒数日列表失败:', error);
      throw error;
    }
  },

  /**
   * 根据ID获取倒数日事件详情
   */
  async getById(id) {
    try {
      const response = await request(`${this.baseUrl}/get/${id}`);
      return response;
    } catch (error) {
      console.error('获取倒数日详情失败:', error);
      throw error;
    }
  },

  /**
   * 添加倒数日事件
   */
  async add(data) {
    try {
      const response = await request(`${this.baseUrl}/add`, {
        method: 'POST',
        data: data
      });
      return response;
    } catch (error) {
      console.error('添加倒数日失败:', error);
      throw error;
    }
  },

  /**
   * 更新倒数日事件
   */
  async update(data) {
    try {
      const response = await request(`${this.baseUrl}/update`, {
        method: 'PUT',
        data: data
      });
      return response;
    } catch (error) {
      console.error('更新倒数日失败:', error);
      throw error;
    }
  },

  /**
   * 删除倒数日事件
   */
  async delete(id) {
    try {
      const response = await request(`${this.baseUrl}/delete/${id}`, {
        method: 'DELETE'
      });
      return response;
    } catch (error) {
      console.error('删除倒数日失败:', error);
      throw error;
    }
  },

  /**
   * 根据创建人获取倒数日事件
   */
  async getByCreator(creator) {
    try {
      const response = await request(`${this.baseUrl}/byCreator/${creator}`);
      return response;
    } catch (error) {
      console.error('根据创建人获取倒数日失败:', error);
      throw error;
    }
  },

  /**
   * 获取即将到期的倒数日事件（7天内）
   */
  async getUpcoming() {
    try {
      const response = await request(`${this.baseUrl}/upcoming`);
      // 统一处理响应格式
      if (Array.isArray(response)) {
        return { code: 200, data: response };
      } else if (response && response.code) {
        return response;
      } else {
        return { code: 200, data: [] };
      }
    } catch (error) {
      console.error('获取即将到期倒数日失败:', error);
      throw error;
    }
  },

  /**
   * 获取已过期的倒数日事件
   */
  async getExpired() {
    try {
      const response = await request(`${this.baseUrl}/expired`);
      // 统一处理响应格式
      if (Array.isArray(response)) {
        return { code: 200, data: response };
      } else if (response && response.code) {
        return response;
      } else {
        return { code: 200, data: [] };
      }
    } catch (error) {
      console.error('获取已过期倒数日失败:', error);
      throw error;
    }
  },

  /**
   * 分页查询倒数日事件
   */
  async getPage(params = {}) {
    try {
      const queryParams = new URLSearchParams({
        current: params.current || 1,
        size: params.size || 10,
        keyword: params.keyword || '',
        creator: params.creator || ''
      });
      
      const response = await request(`${this.baseUrl}/page?${queryParams}`);
      return response;
    } catch (error) {
      console.error('分页查询倒数日失败:', error);
      throw error;
    }
  },

  /**
   * 批量删除倒数日事件
   */
  async batchDelete(ids) {
    try {
      const response = await request(`${this.baseUrl}/batchDelete`, {
        method: 'DELETE',
        data: ids
      });
      return response;
    } catch (error) {
      console.error('批量删除倒数日失败:', error);
      throw error;
    }
  },

  /**
   * 获取倒数日统计信息
   */
  async getStatistics() {
    try {
      const response = await request(`${this.baseUrl}/statistics`);
      return response;
    } catch (error) {
      console.error('获取倒数日统计失败:', error);
      throw error;
    }
  },

  /**
   * 搜索倒数日事件
   */
  async search(keyword) {
    try {
      const response = await request(`${this.baseUrl}/search?keyword=${encodeURIComponent(keyword)}`);
      // 统一处理响应格式
      if (Array.isArray(response)) {
        return { code: 200, data: response };
      } else if (response && response.code) {
        return response;
      } else {
        return { code: 200, data: [] };
      }
    } catch (error) {
      console.error('搜索倒数日失败:', error);
      throw error;
    }
  }
};

module.exports = countdownApi;
